package cn.liangcaomei.educms.controller;


import cn.liangcaomei.commonutils.R;
import cn.liangcaomei.educms.entity.CrmBanner;
import cn.liangcaomei.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台Banner的管理接口控制器
 * </p>
 *
 * @author LiuJie
 * @since 2024-02-04
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {

    //注入 service
    @Autowired
    private CrmBannerService crmBannerService;

    //1 分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit){
        Page<CrmBanner> crmBannerPage = crmBannerService.fetchAllBanner(page, limit);
        return R.ok().data("items",crmBannerPage.getRecords()).data("total",crmBannerPage.getTotal());
    }

    //2 添加Banner
    @ApiOperation(value = "添加Banner")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = crmBannerService.addBanner(crmBanner);
        return flag?R.ok():R.error();
    }

    //3 修改Banner
    @ApiOperation(value = "修改Banner")
    @PostMapping("updateBanner/{id}")
    public R updateBanner(@PathVariable String id, @RequestBody CrmBanner crmBanner){
        boolean flag = crmBannerService.updateBanner(id, crmBanner);
        return flag?R.ok():R.error();
    }

    //4 删除Banner
    @ApiOperation(value = "删除Banner")
    @DeleteMapping("deleteBanner/{id}")
    public R deleteBanner(@PathVariable String id){
        boolean flag = crmBannerService.deleteBanner(id);
        return flag?R.ok():R.error();
    }

    //5 根据id获取Banner
    @GetMapping("get/{id}")
    public R get(@PathVariable String id){
        CrmBanner byIdBanner = crmBannerService.getById(id);
        return R.ok().data("byIdBanner",byIdBanner);
    }
}

